package cn.wolfcode.web.controller;


import cn.wolfcode.domain.*;
import cn.wolfcode.service.*;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;

    @RequiredPermission(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }


    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        //查询门店信息
        model.addAttribute("businesses",businessService.listAll());
        //查询业务大类
        List<SystemDictionaryItem> businessItems = systemDictionaryItemService.selectSystemDictionaryItemBySn("business");
        //查询支付方式
        List<SystemDictionaryItem> payType = systemDictionaryItemService.selectSystemDictionaryItemBySn("pay_type");
        model.addAttribute("businessItems",businessItems);
        model.addAttribute("payType",payType);
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);
            //查询出业务明细
            List<ConsumptionItem>  consumptionItems = consumptionItemService.selectByConsumptionItemCon(consumption.getCno());
            model.addAttribute("consumptionItems",consumptionItems);
        }
        return "consumption/input";
    }

    @RequestMapping("/save")
    public String save(Long id,Integer status){
        //通过预约单的id查询出预约单的信息
        Appointment appointment = appointmentService.get(id);
        //修改预约单状态
        appointmentService.updateStatus(id,status);
        //通过预约单生成结算单,并返回新增结算单的id
        Long consumptionId = consumptionService.saveByAppointment(appointment);
        return "redirect:/consumption/input?id="+consumptionId;
    }

    @RequestMapping("/updateConsume")
    public String updateConsume(Long id,Integer status){
        consumptionService.updateConsume(id,status);
        return "redirect:/consumption/list";
    }

    @RequestMapping("/updateAudit")
    public String updateAudit(Long id,Integer status){
        consumptionService.updateAudit(id,status);
        return "redirect:/consumption/list";
    }
}
